/* *
 * 配置方式
 * 
 *<label id="对应DataPicker的ID" dataPickerUrl="DataPicker页面的URL" width="打开DataPicker窗口的宽度" height="打开DataPicker页面的高度" title="打开的DataPicker窗口标题" >
 *	<input dataPicker="value" name="assessPeriod.id" id="assessPeriod.id" type="hidden" readOnly="readOnly" />
 *	<input dataPicker="name" name="assessPeriod.name" id="assessPeriod.name" type="text" readOnly="readOnly" ondblclick="openDataPicker('对应DataPicker的ID')"/>
 *	<strong onclick="clearDataPicker('对应DataPicker的ID')"></strong>
 *	<span class="p_hov" onclick="openDataPicker('对应DataPicker的ID')"></span>
 *</label>
 *
 * */
function openDataPicker(id){
	var dataPicker = getDataPickerInstance(id);
	if(dataPicker && !dataPicker.disabled){
		dataPicker.show();
	}
}
function clearDataPicker(id){
	var dataPicker = getDataPickerInstance(id);
	if(dataPicker && !dataPicker.disabled){
		dataPicker.clear();
	}
}
function getDataPickerInstance(id){
	var dataPicker = new DataPicker(id);
	return dataPicker;
}
var currentDataPicker;
function returnDataPickerValue(value){
	if(currentDataPicker && currentDataPicker != null){
		currentDataPicker.setValue(value);
	}
}
function DataPicker(id){
	return this.create(id);
}
DataPicker.prototype.create = function(id){
	this.dataPickerid = id;
	this.doc = $('#'+id);
	this.url = this.doc.attr('dataPickerUrl');
	this.title = this.doc.attr('title');
	this.width = this.doc.attr('width');
	this.height = this.doc.attr('height');
	this.isMultiple = this.doc.attr('isMultiple');
	this.disabled = this.doc.attr('disabled')=='disabled'?true:false;
	this.setDisabled(this.disabled);
	this.onValueChange = this.doc.attr('onchange');
	this.oldValue = null;
	this.value = null;;
	var id = this.doc.find('input[dataPicker="value"]').val();
	var name =this.doc.find('input[dataPicker="name"]').val();
	if(id != null && id != ''){
		this.oldValue = {'id':id,'name':name};
		this.value = this.oldValue;
	}
	return this;
}
DataPicker.prototype.show = function(){
	art.dialog.data('returnFunName','returnDataPickerValue');
	currentDataPicker = this;
	var dlg = art.dialog.open(this.url,
			{id:this.dataPickerid,
			 title: this.title,
			 width:this.width,
			 height:this.height,
			 lock: true,
			 button:[{name:'确定',callback:sureValue},{name:'取消'}]
			 }
	);
	function sureValue(){
		if(dlg.iframe.contentWindow && dlg.iframe.contentWindow.okSelect){
			dlg.iframe.contentWindow.okSelect();
		}
		return false;
	}
}
DataPicker.prototype.setValue = function(value){
	var newValue = value;
	if(value && value != null){
		if(value && value.length){//如果是数组
			//传过来的是数组
			if(this.isMultiple){
				//当前为多选DataPicker
				var vs = "";
				var ns = "";
				for(var i = 0; i < value.length; i++){
					vs = (vs==""?value[i].id:vs+";"+value[i].id);
					ns = (ns==""?value[i].name:ns+";"+value[i].name)
				}
				this.doc.find('input[dataPicker="value"]').val(vs);
				this.doc.find('input[dataPicker="name"]').val(ns);
			}else{
				this.doc.find('input[dataPicker="value"]').val(value[0].id);
				this.doc.find('input[dataPicker="name"]').val(value[0].name);
				newValue = value[0];
			}
		}else{
			//传来的是非数组，或者数组长度为0
			this.doc.find('input[dataPicker="value"]').val(value.id);
			this.doc.find('input[dataPicker="name"]').val(value.name);
			if(this.isMultiple){
				newValue = new Array();
				newValue.push(value);
			}
		}
	}else{
		this.doc.find('input[dataPicker="value"]').val("");
		this.doc.find('input[dataPicker="name"]').val("");
	}
	this.oldValue = this.value;
	this.value = newValue;
	if(this.value==null){
		$(this.doc).find('strong').css("display",'none');
	}else{
		$(this.doc).find('strong').css("display",'block');
	}
	if(this.onValueChange && this.onValueChange != '' && this.onValueChange != null){
		window[this.onValueChange].call(null,this.oldValue,newValue,this.doc);
	}
}
DataPicker.prototype.clear = function(){
	this.setValue(null);
}
DataPicker.prototype.setDisabled = function(disabled){
	if(true == disabled){
		this.doc.find('input[dataPicker]').attr('disabled','disabled');
	}else{
		this.doc.find('input[dataPicker]').removeAttr('disabled');
	}
	this.disabled = disabled;
}

$(document).ready(function(){
	$('div[dataPickerUrl]').each(function(){
		var v = $(this).find('input[dataPicker="value"]');
		if(v && (v.val()==null || v.val()=='')){
			$(this).find('strong').css('display','none');
		}else{
			$(this).find('strong').css('display','block');
		}
	});
});
